package com.bocai.freeman.utils;

import android.content.Context;
import android.content.SharedPreferences;


/**
 * Created by admin on 2015/11/17.
 */
public class SP {
    public static String TAG = "SP";
    private static String FILE_NAME = "freeMan";

    /**
     *从sharedpreference中获取数据
     * @param context
     * @param key
     * @param defaultOBJ
     *
     * */
    public static Object get(Context context,String key,Object defaultOBJ){
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
        if(defaultOBJ instanceof String){
            return sp.getString(key, (String) defaultOBJ);
        }else if(defaultOBJ instanceof Integer){
            return sp.getInt(key, (Integer) defaultOBJ);
        }else if(defaultOBJ instanceof Boolean){
            return sp.getBoolean(key, (Boolean) defaultOBJ);
        }else if(defaultOBJ instanceof Long){
            return sp.getLong(key, (Long) defaultOBJ);
        }else if(defaultOBJ instanceof Float){
            return sp.getFloat(key, (Float) defaultOBJ);
        }
        return null;
    }

    /**
     * 在sharedpreference中存储数据
     * @param context
     * @param key
     * @param obj
     *
     * */
    public static void put(Context context, String key, Object obj){
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        if(obj instanceof String){
            editor.putString(key, (String) obj);
        }else if(obj instanceof Boolean){
            editor.putBoolean(key , (Boolean) obj);
        }else if(obj instanceof Float){
            editor.putFloat(key, (Float) obj);
        }else if(obj instanceof Long){
            editor.putLong(key, (Long) obj);
        }else if(obj instanceof Integer){
            editor.putInt(key, (Integer) obj);
        }
        editor.apply();
    }

    /**
     * 清除sharedpreperence所有数据
     * @param context
     * */
    public static void clear(Context context){
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.apply();
    }

    /**
     * 只用于标记引导页
     *
     * */
    public static boolean getFirst(Context context,String key,boolean b){
        SharedPreferences sp = context.getSharedPreferences("first",Context.MODE_PRIVATE);
        return sp.getBoolean(key, b);
    }

    public static void putFirst(Context context, String key, boolean b){
        SharedPreferences sp = context.getSharedPreferences("first",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(key , b);
        editor.apply();
    }

    public static void clenrPosition(Context context,String key){
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        editor.apply();
    }

}
